import json

import pandas as pd
import requests
import certifi
from glom import glom

df = pd.read_json('/Users/yujiahao/02_Xizhi/日常学习（私人勿动）/code/My_Python/input/sites.json ')

print(df.to_string())


# 字典格式的 JSON
s = {
    "col1":{"row1":1,"row2":2,"row3":3},
    "col2":{"row1":"x","row2":"y","row3":"z"}
}

# 读取 JSON 转为 DataFrame
df = pd.DataFrame(s)
print(df)









# URL
URL = 'https://static.jyshare.com/download/sites.json'

# 使用 certifi 提供的证书路径
cert_path = certifi.where()

# 获取 JSON 数据
response = requests.get(URL, verify=cert_path)
data = response.json()

# 转换为 DataFrame
df = pd.DataFrame(data)

# 打印 DataFrame
print(df)




print('----------------------内嵌的 JSON 数据-------------------')
df = pd.read_json('/Users/yujiahao/02_Xizhi/日常学习（私人勿动）/code/My_Python/input/nested_list.json')
print(df)

#这里面都是嵌套的JSON 这时我们就需要使用到 json_normalize() 方法将内嵌的数据完整的解析出来：


# 使用 Python JSON 模块载入数据
with open('/Users/yujiahao/02_Xizhi/日常学习（私人勿动）/code/My_Python/input/nested_list.json','r') as f:
    data = json.loads(f.read())

# 展平数据
df_nested_list = pd.json_normalize(data, record_path =['students'])
print(df_nested_list)



 

df = pd.read_json('/Users/yujiahao/02_Xizhi/日常学习（私人勿动）/code/My_Python/input/nested_deep.json')

data = df['students'].apply(lambda row: glom(row, 'grade.math'))
print(data)